#include <iostream>
#include <unistd.h>
using namespace std;

void PrintLog(pid_t id, int fd)
{
    cout << "child: " << id <<" , fd :" << fd << " , task message : print log" << endl;
}

void ConnectMysql(pid_t id, int fd)
{
    cout << "child: " << id <<" , fd :" << fd << " , task message : connect mysql" << endl;
}

void ReloadConf(pid_t id, int fd)
{
    cout << "child: " << id <<" , fd :" << fd << " , task message : reload conf" << endl;
}

// 子进程进行执行任务
using task_t = void(*)(pid_t, int);
task_t tasks[3] = {PrintLog, ConnectMysql, ReloadConf};

size_t GetTaskID()
{
    return rand() % 3;
}

void work(int fd)
{
    cout << "working...." << endl;
    while (true)
    {
        size_t task_id = 0;
        ssize_t n = read(0, &task_id, sizeof task_id);
        if (n > 0)
        {
            if (task_id < 3)
            {
                tasks[task_id](getpid(), fd);
            }
        }
        else if (n == 0)
        {
            cout << "child work done,quit..." << endl;
            break;
        }
    }
}

